#include <FU68xx_2.h>
#include <Myproject.h>
MotStaType mcState;
MotStaM    McStaSet;
extern MCL_TypeDef     xdata   Ctl;
extern HALL_TypeDef    xdata   HALL;
uint16 Align_time = 0;
static uint8 Delayus(uint16 timer);
extern uint8 xdata  MotorDirection;
//extern uint32 xdata   HALL_Stall_time;
extern uint8  xdata  I_Limt_Flag;

void MC_Control(void)
{
    switch(mcState)
    {
        case mcReady:  
				{
					if(mcSpeedRamp.FlagONOFF == 1)
					{
						mcState = mcInit;
						HALL_Stall_time = 0;
						I_Limt_Flag = 0;
				  }
				}
        break;
				

        case mcInit:                          
				{
						
					MotorcontrolInit();
					PI_Init();	
				//	MotorDirection = CW;			
				//	SetnSleep_IOPin;
					MOE     = 0;                     
                    DRV_CMR = 0X0540;
					DRV_DR = 0;
					mcState                   =  mcCharge;          
				}
        break;

        case mcCharge:                     
				{			
					Motor_Charge();
				#if (IPMState == IPMtest) 	        
			       {	   
				       ;
			       }
				  #elif (IPMState == NormalRun)         
						 {
							if( mcFocCtrl.State_Count == 0)
							{
								MOE     = 1;                  
                                DRV_CMR = 0X0540;
								DRV_DR = 0;
								mcState = mcAlign;						
							}
					   }
					#endif 
				 }
        break;
			
				case mcAlign:                 
				{						
					 Hall_IRQHandler();
					 DRV_HallEventCalc();
					 mcSpeedRamp.BLDC_Value = RAMP_target_STA;  // deleted by shiyan
                     DRV_DR = RAMP_target_STA;					
					 mcState = mcBLDC;
				}
				break;
				
				case mcBLDC:                 
				{
	   		  mcState = mcRun;            
				}
        break;

        case mcRun:                        
				{
					if(mcSpeedRamp.FlagONOFF == 0)
					{
						mcState  = mcStop;
					}		
				}
        break;

        case mcStop:
				{
					MOE        = 0;		
                   TIM1_CR4=7;					
					Driver_Init();	 
				//	ClrBit(TIM2_CR1,T2EN);
			    	ClrBit(TIM1_IER, T1PDIE); 
					ClrBit(DRV_CR, OCS);					
					DRV_DR   = 0;
					DRV_CMR  = 0x0000;    
					MOE        = 1;				
					mcState = mcBrake;			
         // ONOFFTest.ONOFF_Flag=1;
         // ONOFFTest.ONOFF_Times=0;	
         // ONOFFTest.OFF_Count=0;
                   ONOFFTest.PowerOFF_Count = 0;					
				}
        break;

        case mcBrake:
				{	
           if ((mcSpeedRamp.FlagONOFF == 1) && (ONOFFTest.PowerOFF_Count >= 50))
            {
				ONOFFTest.PowerOFF_Count = 50;
                mcState = mcReady;
                McStaSet.SetFlag.CalibFlag = 0;
            }    																						
				}
        break;

        case mcFault:
			{
//          if((mcSpeedRamp.FlagONOFF == 0)&&( mcFaultSource != FaultMosOverTemperature)
//						&&(mcFaultSource != FaultBATOverTemperature)&&(mcFaultSource != FaultMOTOverTemperature)
//					  &&(mcFaultSource != FaultUnderVoltage)&&(mcFaultSource != FaultOverVoltage))
	         if (mcSpeedRamp.FlagONOFF == 0)
			 {
						//	mcFaultSource = FaultNoSource; 
			  	mcState = mcBrake;
	         }
        }
        break;
    }
}



